package com.shinhye.elephantsafe.utils;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.shinhye.elephantsafe.R;

public class CreatePSWUtils {
	private static AlertDialog dialog;
	private static SharedPreferences sp;
	
	/**
	 * A interface to indicate what to do after button in the Password Dialog has been clicked.
	 * @author ShinHye
	 *
	 */
	public interface PswBtn {
		/**
		 * OK button has been clicked.
		 */
		void onOkBtn();
		/**
		 * Cancel button has been clicked.
		 */
		void onCanceledBtn();
	}
	
	/**
	 * Show a dialog to create or enter ElephantSafe password.
	 * @param context
	 * @param ifEnterPSW If to pop up an enter password dialog while there is a ElephantSafe password exist.
	 * @param pswbtn A CallBack when click the button.
	 */
	public static void showPSWDialog(Context context, boolean ifEnterPSW, PswBtn pswbtn) {

		if(hasPSWSetted(context)) {
			if(ifEnterPSW)
				showEnterPSWDialog(context, pswbtn);
		} else {
			showSetPSWDialog(context, pswbtn);
		}
	}
	
	/**
	 * Show a dialog to set up the password.
	 * @param context
	 * @param clazz After enter or create password which activity should turn to.
	 * @param pswbtn A CallBack when click the cancel button.
	 */
	public static void showSetPSWDialog(final Context context, final PswBtn pswbtn) {
		Builder builder = new Builder(context);
		
		View view = View.inflate(context, R.layout.dialog_setpsw, null);
		final EditText et_psw = (EditText) view.findViewById(R.id.setpsw);
		final EditText et_psw_confirm = (EditText) view.findViewById(R.id.setpsw_confirm);
		view.findViewById(R.id.setpsw_btn_ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String psw = et_psw.getText().toString();
				String psw_confirm = et_psw_confirm.getText().toString();
				if(TextUtils.isEmpty(psw) || TextUtils.isEmpty(psw_confirm)) {
					Toast.makeText(context, "密码或确认密码不能为空！请重新输入", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!psw.equals(psw_confirm)) {
					Toast.makeText(context, "两次输入的密码不一致！", Toast.LENGTH_SHORT).show();
				} else {
					Editor editor = sp.edit();
					editor.putString("password", MD5Utils.md5(psw));
					editor.commit();
					
					dialog.dismiss();
					pswbtn.onOkBtn();
				}
			}
		});
		view.findViewById(R.id.setpsw_btn_cancel).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				pswbtn.onCanceledBtn();
			}
		});
		
		//改善Android2.3的显示效果问题
//		builder.setView(view);
//		dialog = builder.show();
		dialog = builder.create();
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();
	}

	/**
	 * Show a dialog to enter password.
	 * @param context
	 * @param clazz After enter or create password which activity should turn to.
	 * @param pswbtn A CallBack when click the cancel button.
	 */
	public static void showEnterPSWDialog(final Context context, final PswBtn pswbtn) {
		Builder builder = new Builder(context);
		
		View view = View.inflate(context, R.layout.dialog_enterpsw, null);
		final EditText et_psw = (EditText) view.findViewById(R.id.enterpsw);
		view.findViewById(R.id.enterpsw_btn_ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String psw = sp.getString("password", "");
				String enter_psw = et_psw.getText().toString();
				if(TextUtils.isEmpty(enter_psw)) {
					Toast.makeText(context, "密码不能为空！请重新输入！", Toast.LENGTH_SHORT).show();
					return;
				} 
				
				enter_psw = MD5Utils.md5(enter_psw);
				if(psw.equals(enter_psw)) {
					dialog.dismiss();
					pswbtn.onOkBtn();
				} else {
					Toast.makeText(context, "密码输入错误！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		view.findViewById(R.id.enterpsw_btn_cancel).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				pswbtn.onCanceledBtn();
			}
		});
		
		dialog = builder.create();
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();
	}

	/**
	 * Judge if there is password setup in your device.
	 * @param context
	 * @return boolean
	 */
	public static boolean hasPSWSetted(Context context) {
		sp = context.getSharedPreferences("Setting", Context.MODE_PRIVATE);
		String psw = sp.getString("password", "");
		return !TextUtils.isEmpty(psw);
	}

}
